package com.hwhhhh.mygrad.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hwhhhh.mygrad.R;

public class BottomHintFragment extends DialogFragment {
    private static final String OPTIONS = "options";
    private static final String REQUEST_CODE = "request_code";

    private View rootView;
    private RecyclerView rvHint;
    private TextView tvCancel;

    /**
     * 构造底部弹出的对话框
     * @param opts 选项名的组成
     * @param requestCode 请求码，用于区分不同的dialog
     * @return 返回BottomHingFragment
     */
    public static BottomHintFragment newInstance(String[] opts, int requestCode) {
        BottomHintFragment fragment = new BottomHintFragment();
        Bundle bundle = new Bundle();
        bundle.putStringArray(OPTIONS, opts);
        bundle.putInt(REQUEST_CODE, requestCode);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_bottom_hint, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        rvHint = rootView.findViewById(R.id.rv_bottom_hint);
        tvCancel = rootView.findViewById(R.id.tv_cancel);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        String[] ops = getArguments().getStringArray(OPTIONS);
        int requestCode = getArguments().getInt(REQUEST_CODE);
        // 设置底部弹出的选项
        rvHint.setAdapter(new RecyclerView.Adapter<BottomHintHolder>() {
            @NonNull
            @Override
            public BottomHintHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bottom_hint, parent, false);
                return new BottomHintHolder(itemView);
            }

            @Override
            public void onBindViewHolder(@NonNull BottomHintHolder holder, int position) {
                // 设置选项名
                holder.tvOpt.setText(ops[position]);
                // 设置选项的点击事件
                holder.tvOpt.setOnClickListener(v -> ((BottomHintCallBack)getActivity()).doPositive(position, requestCode));
            }


            @Override
            public int getItemCount() {
                return ops.length;
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        rvHint.setLayoutManager(layoutManager);
        // 点击取消时的回调
        tvCancel.setOnClickListener(v -> ((BottomHintCallBack)getActivity()).doNegative(requestCode));
    }

    @Override
    public void onStart() {
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;//设置宽度为铺满
        params.gravity = Gravity.BOTTOM;
        getDialog().getWindow().setAttributes(params);
        // 使背景显示
        getDialog().getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
        super.onStart();
    }

    class BottomHintHolder extends RecyclerView.ViewHolder {
        TextView tvOpt;

        public BottomHintHolder(@NonNull View itemView) {
            super(itemView);
            tvOpt = itemView.findViewById(R.id.tv_bottom_hint_opt);
        }
    }

    public interface BottomHintCallBack {
        void doPositive(int position, int requestCode) ;
        void doNegative(int requestCode);
    }
}
